For-In Loops
Close range
1 | for index in 1...5 { |
Ignore the values
1 | // ignore the values by using an underscore |
Iterate an array
1 | let names = ["Anna", "Alex", "Brian", "Jack"] |
Iterate over a dictionary
Items in a Dictionary may not necessarily be iterated in the same order in which they were inserted
1 | let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4] |
While Loops
While
1 | while condition { |
Repeat-While
1 | repeat { |
Conditional Statements
If
The final else clause is optional
1 | temperatureInFahrenheit = 90 |
Switch
- Every switch statement must be exhaustive
- define a default case to cover
1 | let someCharacter: Character = "z" |
No Implicit Fallthrough
the entire switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement.
at least one executable statement
1 | let anotherCharacter: Character = "a" |
Interval Matching
1 | let approximateCount = 62 |
Tuples
- underscore character (_), also known as the wildcard pattern, to match any possible value.
- Unlike C, Swift allows multiple switch cases to consider the same value or values.
- the first matching,other matching cases would be ignored.
1 | let somePoint = (1, 1) |
Value Bindings
A switch case can bind the value or values it matches to temporary constants or variables
1 | let anotherPoint = (2, 0) |
Where
- A switch case can use a where clause to check for additional conditions.
- switch case matches the current value of point only if the where clause’s condition evaluates to true for that value.
1 | let yetAnotherPoint = (1, -1) |
Compound Cases
1 | let someCharacter: Character = "e" |
Compound cases + value bindings
All of the patterns of a compound case have to include the same set of value bindings
1 | let stillAnotherPoint = (9, 0) |
Control Transfer Statements
Continue
I am done with the current loop iteration,jump straight to the start of the next iteration
1 | let puzzleInput = "great minds think alike" |
Break
The break statement ends execution of an entire control flow statement immediately.
1 | let numberSymbol: Character = "三" // Chinese symbol for the number 3 |
Fallthrough
The fallthrough keyword does not check the case conditions for the switch case that it causes execution to fall into. The fallthrough keyword simply causes code execution to move directly to the statements inside the next case (or default case) block, as in C’s standard switch statement behavior.
1 | let integerToDescribe = 5 |
Labeled Statements
- In nested loop or conditional statement,explicit about which loop or conditional statement you want a break statement to terminate.
- Using the gameLoop label makes it clear which control statement should be terminated.
1 | label name: while condition { |
Early Exit
a condition must be true
1 | func greet(person: [String: String]) { |
Checking API Availability
You use an availability condition in an if or guard statement to conditionally execute a block of code, depending on whether the APIs you want to use are available at runtime
1 | if #available(platform name version, ..., *) { |